"""
persist.py

This file is part of a program that implements a Software-Defined Radio.

Copyright (C) 2014 by G3UKB Bob Cowdery

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The authors can be reached by email at:

	bob@bobcowdery.plus.com

"""

import os,sys
from PyQt4 import QtCore, QtGui
import pickle

"""

Utility functions to get and save state for the various components

"""

def getSavedState(path):
	"""
	Restore the saved state
	
	Arguments:
		path    -- path to state file
		
	"""
	
	state = None
	if os.path.exists(path):
		try:       
			f = open(path, 'rb')
			state = pickle.load(f)
		except Exception as e:
			# Error retrieving state file
			QtGui.QMessageBox.information(None, 'State File - Exception','Exception [%s]' % (str(e)), QtGui.QMessageBox.Ok)
		finally:
			try:
				f.close()
			except:
				pass
	return state

def getSavedCfg(path):
	"""
	Restore the saved configuration
	
	Arguments:
		path    -- path to configuration file
		
	"""
	
	cfg = None
	if os.path.exists(path):
		try:       
			f = open(path, 'rb')
			cfg = pickle.load(f)
		except Exception as e:
			# Error retrieving state file
			QtGui.QMessageBox.information(None, 'Configuration File - Exception','Exception [%s]' % (str(e)), QtGui.QMessageBox.Ok)
		finally:
			try:
				f.close()
			except:
				pass
	return cfg

def saveState(path, state):
	"""
	Save the state
	
	Arguments:
		path    -- path to state file
		state   -- state to save
		
	"""
	
	try:       
		f = open(path, 'wb')
		pickle.dump(state, f)
	except Exception as e:
		# Error saving state file
		QtGui.QMessageBox.information(self, 'State File - Exception','Exception [%s]' % (str(e)), QtGui.QMessageBox.Ok)
	finally:
		try:
			f.close()
		except:
			pass
		
def saveCfg(path, cfg):
	"""
	Save the configuration
	
	Arguments:
		path    -- path to state file
		cfg   	-- state to save
		
	"""
	
	try:       
		f = open(path, 'wb')
		pickle.dump(cfg, f)
	except Exception as e:
		# Error saving configuration file
		QtGui.QMessageBox.information(self, 'Configuration File - Exception','Exception [%s]' % (str(e)), QtGui.QMessageBox.Ok)
	finally:
		try:
			f.close()
		except:
			pass    